<template>
  <div class="api">
    <!--搜索区域-->
    <el-row v-if="false">
      <el-col :span="22">
        <span style="font-size: 14px;font-weight: 600">角色名称: &nbsp;</span>
        <el-input v-model="params.roleName" style="width: 160px" placeholder="请输入角色名称" clearable></el-input>
        <span class="so">角色别名: &nbsp;</span>
        <el-input v-model="params.roleAlias" style="width: 160px" placeholder="请输入角色别名" clearable></el-input>
      </el-col>
      <!--查询按钮-->
      <el-col :span="2">
        <el-button type="primary" plain style="width: 6vw" icon="Search" @click="getDataList()">查询</el-button>
      </el-col>
    </el-row>
    <!--操作区域-->
    <el-row >
      <el-col :span="24">
        <el-button type="primary" plain icon="Plus" @click="drawerAdd" v-if="buttonPermissions('dept:add')">新增</el-button>
        <el-button type="success" plain icon="Download" @click="exportExcel()" v-if="buttonPermissions('dept:export')">导出</el-button>
        <el-button type="danger" plain icon="DeleteFilled" @click="dataDeleteList()" v-if="buttonPermissions('dept:delete')">删除</el-button>
      </el-col>
    </el-row>
    <!--表格区域-->
    <el-row style="margin-top: 25px">
      <el-col :span="24">
        <el-table
          row-key="id"
          :tree-props="{children: 'children'}"
          @selection-change="handleSelectionChange"
          :data="tableData" style="width: 100%"
          :header-cell-style="{'background-color':'#F2F6FC'}" empty-text="暂无数据">
          <el-table-column type="selection" width="55"> </el-table-column>
          <el-table-column prop="deptName" label="部门名称" > </el-table-column>
          <el-table-column prop="deptCategoryName" label="部门类型" > </el-table-column>
          <el-table-column prop="fullName" label="部门全称" > </el-table-column>
          <el-table-column prop="sort" label="排序" > </el-table-column>
          <el-table-column label="操作" align="center" width="220">
            <template #default="scope">
              <el-button @click="drawerUpdate(scope.row)" type="text" icon="edit" v-if="buttonPermissions('dept:update')">编辑</el-button>
              <el-popconfirm title="确认删除该项数据吗？" @confirm="dataDelete(scope.row.id)" v-if="buttonPermissions('dept:delete')">
                <template #reference>
                  <el-button
                    icon="DeleteFilled"
                    style="color: red"
                    type="text">删除</el-button>
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
    <el-row style="margin-top: 15px" justify="end" v-if="false">
      <el-col >
        <el-pagination
          background
          style="float: right"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="params.current"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="params.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </el-col>
    </el-row>
    <!--侧边抽屉-->
    <el-drawer :title="drawer.title" v-model="drawer.enable"  destroy-on-close>
      <data-add :dept-tree="tableData" @tao="subassembly" v-if="drawer.add"></data-add>
      <data-details v-if="drawer.details" :props-data="drawer.data"></data-details>
      <data-update @tao="subassembly" v-if="drawer.update" :props-data="drawer.data" :dept-tree="tableData"></data-update>
    </el-drawer>
  </div>
</template>

<script>
import DataAdd from '@/components/dept/DataAdd'
import DataDetails from '@/components/dept/DataDetails'
import DataUpdate from '@/components/dept/DataUpdate'
import { ElMessage } from 'element-plus'
export default {
  name: 'Index',
  components:{
    DataUpdate,
    DataDetails,
    DataAdd
  },
  data () {
    return {
      params: {
        current: 1,
        size: 10,
        roleName: '',
        roleAlias: ''
      },
      drawer:{
        title:'',
        enable:false,
        add:false,
        details:false,
        update:false,
        data:null
      },
      total:0,
      tableData: [],
      selectId: []
    }
  },
  computed:{
    //本页面按钮集合,表示该用户在该页面拥有的按钮
    buttonList(){
      let buttonList = this.$store.getters.buttonList
      //过滤获得当前页面配置
      let filter = buttonList.filter(data => data.path === this.$route.path)
      //获得实际按钮
      let buttons = filter.length === 0 ? null : filter[0].children
      if (buttons === null){
        return null
      }
      //获得按钮权限标记
      return buttons.map(data => data.permissionId)
    }
  },
  mounted () {
    this.getDataList()
  },
  methods:{
    //按钮权限判断
    buttonPermissions(buttonTag){
      return  this.buttonList === null ? false :this.buttonList.indexOf(buttonTag) >= 0
    },
    //获得页面数据
    getDataList(){
      this.$http.get('/api/dept/tree',{params:this.params}).then(res =>{
        this.tableData = res.data.data
      })
    },
    //表格数据选中
    handleSelectionChange(val){
     this.selectId = val.map(data => data.id)
    },
    // 批量数据删除
    dataDeleteList(){
      if (this.selectId.length === 0){
        ElMessage.error('请至少选中一项数据')
      }
      //转成逗号分割的字符串
      this.dataDelete( this.selectId.reduce(((previousValue, currentValue) => previousValue + "," + currentValue)))
    },
    // excel导出
    exportExcel(){
      this.$http.get('/api/dept/importExcel',{responseType:'blob'}).then(res =>{
        //获取生成设置好的文件名
        let filename = res.headers["content-disposition"].split("=")[1]
        if (filename){
          //文件名解码
          filename = decodeURIComponent(filename);
          //创建blob对象
          const blob = new Blob([res.data]);
          //创建a标签
          let link = document.createElement("a");
          let url = URL.createObjectURL(blob);
          link.style.display = "none";
          link.href = url;
          link.setAttribute("download", filename);
          document.body.appendChild(link);
          link.click();
          //释放资源
          window.URL.revokeObjectURL(url);
          document.body.removeChild(link);
        }
      })
    },
    // 单条数据删除
    dataDelete(id){
      this.$http.delete('/api/dept/delete?idList='+id).then(res =>{
        ElMessage({
          type: 'success',
          message: res.data.message,
        })
        this.getDataList()
      })
    },
    // 分页-页面大小
    handleSizeChange(val) {
     this.params.size = val
      this.getDataList()
    },
    // 分页-当前页面
    handleCurrentChange(val) {
      this.params.current = val
      this.getDataList()
    },
    // 子组件新增/更新成功
    subassembly(){
      //关闭全部抽屉
      this.closure()
      //更新数据
      this.getDataList()
    },
    //关闭抽屉
    closure(){
      this.drawer.enable = false;
      this.drawer.add = false;
      this.drawer.update = false;
      this.drawer.details = false;
      this.drawer.data = null
    },
    //抽屉-新增
    drawerAdd(){
      this.closure()
      this.drawer.title = '数据新增'
      this.drawer.enable = true;
      this.drawer.add = true;
    },
    //抽屉-详情
    drawerDetails(data){
      this.closure()
      this.drawer.title = '数据详情'
      this.drawer.enable = true;
      this.drawer.details = true;
      this.drawer.data = data
    },
    //抽屉-更新
    drawerUpdate(data){
      this.closure()
      this.drawer.title = '数据更新'
      this.drawer.enable = true;
      this.drawer.update = true;
      this.drawer.data = data
    }
  }
}
</script>

<style scoped>
.so {
  margin-left: 10px;
  font-size: 14px;
  font-weight: 600;
}
.api {
  width: 100%;
}
</style>
